package shop.controller.manage.store;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.BaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.core.common.bean.ManageContainer;
import shop.core.common.dao.page.PagerModel;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by lee on 2017/10/17.
 */
@Controller
@RequestMapping("/manage/store/")
public class StoreAction extends BaseController<User> {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(StoreAction.class);

    private static final String page_toList = "/manage/store/storeList";
    private static final String page_toEdit = "/manage/store/storeDetail";

    public StoreAction() {
        super.page_toList = page_toList;
        super.page_toEdit = page_toEdit;
        super.page_toAdd = null;
    }

    @Autowired
    private UserService userService;

    @Override
    public Services<User> getService() {
        return userService;
    }

    /**
     * 跳转到店铺信息的方法
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping("toStoreDetail")
    public String toStoreDetail(HttpServletRequest request,ModelMap modelMap){
        User loginUser=(User)request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        User user=userService.selectById(loginUser.getId());
        modelMap.addAttribute("e",user);
        return page_toEdit;
    }

    /**
     * 改变店铺状态
     * @param request
     * @param e
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateStatus(HttpServletRequest request, @ModelAttribute("e") User e) throws Exception {
        JSONResult jsonResult = new JSONResult();
        if(e.getStatus().equals("0")){
            e.setStatus("n");
        }else {
            e.setStatus("y");
        }
        userService.update(e);
        jsonResult.setSuccess(true);
        return jsonResult;
    }
}
